package com.xiaoxu.rental.controller;

import com.xiaoxu.rental.entity.Dept;
import com.xiaoxu.rental.service.DeptService;
import com.xiaoxu.rental.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @author xiaoxu
 * @since 2024-07-24
 */
@Api(tags = "部门管理")
@RestController
@RequestMapping("/rental/dept")
@SuppressWarnings("all")
public class DeptController {
    @Resource
    private DeptService deptService;

    @ApiOperation(value = "部门列表")
    @PostMapping("/list")
    public Result list(@RequestBody Dept dept){
        return Result.success().setData(deptService.selectList(dept));
    }
    @ApiOperation(value = "部门树")
    @GetMapping("/trees")
    public Result selectTree(){
        return Result.success().setData(deptService.selectTree());
    }

    @ApiOperation(value = "新增部门")
    @PostMapping("/save")
    public Result save(@RequestBody Dept dept){

        return  deptService.save(dept)? Result.success(): Result.fail();
    }

    @ApiOperation(value = "修改部门")
    @PutMapping("/update")
    public Result update(@RequestBody Dept dept){
        return  deptService.updateById(dept)? Result.success(): Result.fail();
    }

    @ApiOperation(value = "删除部门")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        return deptService.removeById(id)?Result.success():Result.fail();
    }

    @ApiOperation(value = "是否有子部门")
    @GetMapping("/hasChildren/{id}")
    public Result hasChildren (@PathVariable Integer id){

        return deptService.hasChildren(id)?
                Result.success().setMessage("有"):
                Result.success().setMessage("无");
    }


}
